{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ec2bd71d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shape表示数组的形状\n",
      "(3,)\n",
      "(2, 3)\n",
      "(2, 2, 2)\n",
      "ndim表示数组的维度\n",
      "1\n",
      "2\n",
      "3\n",
      "size表示数组中的元素数量\n",
      "3\n",
      "6\n",
      "8\n",
      "itemsize表示一个数组元素的长度(字节)\n",
      "4\n",
      "8\n",
      "8\n",
      "dtype表示元组元素的类型\n",
      "int32\n",
      "int64\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "   \n",
    "a = np.array([1,2,3], dtype=np.int32)   # 可以手动指定数据类型\n",
    "b = np.array([[1,2,3],[4,5,6]])\n",
    "c = np.array([[[1,2],[3,4]], [[5,6],[7,8]]])\n",
    "#shape表示数组的形状\n",
    "print(\"shape表示数组的形状\")\n",
    "print(a.shape)\n",
    "print(b.shape)\n",
    "print(c.shape)\n",
    "\n",
    "#ndim表示数组的维度\n",
    "print(\"ndim表示数组的维度\")\n",
    "print(a.ndim)\n",
    "print(b.ndim)\n",
    "print(c.ndim)\n",
    "\n",
    "#size表示数组中的元素数量\n",
    "print(\"size表示数组中的元素数量\")\n",
    "print(a.size)\n",
    "print(b.size)\n",
    "print(c.size)\n",
    "\n",
    "#itemsize表示一个数组元素的长度(字节)\n",
    "print(\"itemsize表示一个数组元素的长度(字节)\")\n",
    "print(a.itemsize)\n",
    "print(b.itemsize)\n",
    "print(c.itemsize)\n",
    "\n",
    "#dtype表示元组元素的类型\n",
    "print(\"dtype表示元组元素的类型\")\n",
    "print(a.dtype)\n",
    "print(b.dtype)\n",
    "print(c.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e5ebe260",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "d = np.dtype(np.int32)  # 创建一个dtype实例\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ac971ee1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 2.]\n",
      " [3. 4.]]\n",
      "[[1. 2.]\n",
      " [3. 4.]]\n"
     ]
    }
   ],
   "source": [
    "# 创建数组指定数据类型\n",
    "e = np.array([[1,2],[3,4]],dtype=np.float32)\n",
    "print(e)\n",
    "\n",
    "f = np.array([[1,2],[3,4]],dtype=np.dtype('f4'))\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "23dd54b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组形状\n",
      "(3, 2)\n",
      "(3, 2)\n",
      "数组维度\n",
      "2\n",
      "2\n",
      "数组元素类型\n",
      "int32\n",
      "float64\n",
      "元素占用字节数\n",
      "4\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "g = np.array([[1, 2], [3, 4], [5, 6]], dtype=np.int32)\n",
    "h = np.array([[10, 20], [30, 40], [50, 60]], dtype=np.dtype(np.float64))\n",
    "\n",
    "print(\"数组形状\")\n",
    "print(g.shape)\n",
    "print(h.shape)\n",
    "\n",
    "print(\"数组维度\")\n",
    "print(g.ndim)\n",
    "print(h.ndim)\n",
    "\n",
    "print(\"数组元素类型\")\n",
    "print(g.dtype)\n",
    "print(h.dtype)\n",
    "\n",
    "print(\"元素占用字节数\")\n",
    "print(g.itemsize)\n",
    "print(h.itemsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "36c3a243",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "自定义的价格数据\n",
      "[(10. ,) (25.8,) (36.6,)]\n",
      "自定义的购物数据\n",
      "[(b'meat', 15.6, 2) (b'apple',  6. , 2)]\n"
     ]
    }
   ],
   "source": [
    "# 创建结构化数据类型\n",
    "\n",
    "# 创建数据类型\n",
    "price = np.dtype([('price','f4')]) # price 类型的字段名，自定义, 'f4'数据类型 float32\n",
    "\n",
    "# 将数据类型应用到 ndarray对象\n",
    "price_data = np.array([10,25.8,36.6], dtype=price)\n",
    "print(\"自定义的价格数据\")\n",
    "print(price_data)\n",
    "\n",
    "#创建数组中不同的数据类型数组\n",
    "goods = np.dtype([('name','S20'), ('price', 'f4'),\n",
    "               ('weight', 'i1')])\n",
    "goods_data = np.array([('meat', 15.6, 2),('apple', 6, 2)],dtype=goods)\n",
    "print(\"自定义的购物数据\")\n",
    "print(goods_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "235be8ab",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python-data-analysis",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
